package com.guigu.pd.work.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.guigu.common.utils.PageVo;
import com.guigu.pd.auth.entity.AuthUserEntity;
import com.guigu.pd.serviceapi.authfeign.UserFeign;
import com.guigu.pd.serviceapi.pojo.TaskPickupDispatch;
import com.guigu.pd.work.common.CustomIdGenerator;
import com.guigu.pd.work.entity.TransportOrderEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.guigu.pd.serviceapi.pojo.TaskPickupDispatchEntity;
import com.guigu.pd.work.service.TaskPickupDispatchService;
import com.guigu.common.utils.PageUtils;
import com.guigu.common.utils.R;



/**取件、派件任务信息表
 *
 *
 * @author deyran
 * @email deyran@gmail.com
 * @date 2022-07-19 19:31:34
 */
@RestController
@RequestMapping("work/taskpickupdispatch")
public class TaskPickupDispatchController {
    @Autowired
    private TaskPickupDispatchService taskPickupDispatchService;

    @Autowired
    UserFeign userFeign;

    @Autowired
    private CustomIdGenerator customIdGenerator;

    /**
     * 取件列表
     */
    @RequestMapping("/qjlist")
    public R QJlist(@RequestParam Map<String, Object> params) throws ParseException {
        PageVo<TaskPickupDispatchEntity> page = taskPickupDispatchService.queryQJtaskPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 派件列表
     */
    @RequestMapping("/pjlist")
    public R PJlist(@RequestParam Map<String, Object> params){
        PageVo<TaskPickupDispatchEntity> page = taskPickupDispatchService.queryPJtaskPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id){
		TaskPickupDispatchEntity taskPickupDispatch = taskPickupDispatchService.getById(id);

        return R.ok().put("taskPickupDispatch", taskPickupDispatch);
    }

    /**
     * 保存
     */
    @PostMapping("/savekdylanjian")
    public boolean savekdylanjian(@RequestBody TaskPickupDispatch taskPickupDispatch){

        TaskPickupDispatchEntity taskPickupDispatchEntity = new TaskPickupDispatchEntity();
        BeanUtils.copyProperties(taskPickupDispatch,taskPickupDispatchEntity);

        taskPickupDispatchEntity.setId(customIdGenerator.nextId(taskPickupDispatchEntity)+"");

        boolean save = taskPickupDispatchService.save(taskPickupDispatchEntity);

        return save;
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody TaskPickupDispatchEntity taskPickupDispatch){
		taskPickupDispatchService.updateById(taskPickupDispatch);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete/{ids}")
    public R delete(@PathVariable(name = "ids")String[] ids){
        for (String id : ids) {
            taskPickupDispatchService.removeById(id);
        }

        return R.ok();
    }


    /**
     * 根据快递员信息和派件取件status 状态判断那个快递员为该订单的收件人
     */
    @GetMapping("/panduanshoujiankdy/{id}")
    public int panduanshoujiankdy(@PathVariable(name = "id")String id){

        String[] kdyid = id.split(",");

        int number = 5;
        int xiabiao = -1;

        for(int i = 0;i<kdyid.length;i++){
            LambdaQueryWrapper<TaskPickupDispatchEntity> lambdaQueryWrapper = new LambdaQueryWrapper<TaskPickupDispatchEntity>();

            lambdaQueryWrapper.eq(TaskPickupDispatchEntity::getCourierId,kdyid[i]).eq(TaskPickupDispatchEntity::getSignStatus,2);

            List<TaskPickupDispatchEntity> list = taskPickupDispatchService.list(lambdaQueryWrapper);


            //未执行完的取件个数 如果小于下一次的个数就进去
            //附下标值
            //如果都大于五个就返回负一手动安排快递员收快递
            if(list.size()<number){
                number = list.size();
                    xiabiao = i;
            }
        }

        return xiabiao;
    }


    /**
     * 分配快递员
     */
    @RequestMapping("/makecourier")
    public R makeCourier(String cid,String ids){
        UpdateWrapper<TaskPickupDispatchEntity> updateWrapper = new UpdateWrapper<TaskPickupDispatchEntity>();
        String[] idstr = ids.split(",");
        for (String id : idstr) {
            updateWrapper.eq("id",Integer.parseInt(id)).set("courier_id",cid).set("assigned_status",2);
        }
        taskPickupDispatchService.update(updateWrapper);

        return R.ok();
    }

    /**
     * 查询快递员
     */
    @RequestMapping("/querycourier")
    public R queryCourier(){
        Map<String, String> couriermap = userFeign.getallCourier();
        List<AuthUserEntity> courier = new ArrayList<AuthUserEntity>();
        for(Map.Entry<String, String> entry:couriermap.entrySet()){
            AuthUserEntity user = new AuthUserEntity();
            user.setId(entry.getKey());
            user.setName(entry.getValue());
            courier.add(user);
        }
        System.out.println(courier);
        return R.ok().put("courier",courier);
    }

    @RequestMapping("/updatestatus/{id}")
    public R updatestatusbyId(@PathVariable("id") Integer id){
        UpdateWrapper<TaskPickupDispatchEntity> updateWrapper = new UpdateWrapper<TaskPickupDispatchEntity>();
        updateWrapper.eq("id",id).set("status",4);
        taskPickupDispatchService.update(updateWrapper);

        return R.ok();
    }

    /**
     * 取消列表
     */
    @RequestMapping("/cjlist")
    public R CJlist(@RequestParam Map<String, Object> params){
        PageVo<TaskPickupDispatchEntity> page = taskPickupDispatchService.queryCJtaskPage(params);

        return R.ok().put("page", page);
    }

    @GetMapping("/getbyorderid/{id}")
    public TaskPickupDispatchEntity getbyorderId(@PathVariable("id") String id){
        QueryWrapper<TaskPickupDispatchEntity> queryWrapper = new QueryWrapper<TaskPickupDispatchEntity>();
        queryWrapper.eq("order_id",id);
        TaskPickupDispatchEntity pickupDispatch = taskPickupDispatchService.getOne(queryWrapper);

        return pickupDispatch;
    }

}
